import { uuid } from '@antv/x6/lib/util/string/uuid'

/**
 * 表单设计数据库属性
 */

// 列表显示的对象数组
const state = {
  i: 0,
  multipleTable: [],
  dbPropertyList: [],
  tableHeight: '',
  indexMemory: {},
  rowData: {},
  head: {}, // 表信息
  tempId: ''
}

const mutations = {
  ADD_DB_PROPERTY(state, value) {
    state.dbPropertyList.push(value)
    // 维护添加的索引
    state.i = 1
    state.dbPropertyList.forEach(e => {
      e.index = state.i++
      if (!e.id) {
        e.id = uuid().replaceAll('-', '') + 'add'
      }
      console.log(e.id, 'eee')
    })
  },
  DEL_DB_PROPERTY(state, value) {
    // value 是要删除的数组
    value.forEach(e => {
      state.dbPropertyList.forEach((v) => {
        if (v.index === e.index) {
          state.dbPropertyList.splice(e.index - 1, 1)
          state.i = 1
          state.dbPropertyList.forEach(e => { e.index = state.i++ })
        }
      })
    })
  },
  INIT_DB_PORPERTY(state, value) {
    // 重置索引
    state.i = 1
    if (value.length !== 0) {
      value.forEach(e => {
        e.dbIsNull = !(e.dbIsNull === 0)
      })
      state.dbPropertyList = value
      // state.dbPropertyList.forEach(e => { e.dbIsNull = !(e.dbIsNull === 0) })
    } else {
      // 初始五个字段
      const arr = [
        {
          dbFieldName: 'id',
          dbLength: '64',
          dbPointLength: '0',
          dbDefaultVal: '',
          dbFieldTxt: '主键id',
          dbIsNull: false,
          dbType: 'string'
        },
        {
          dbFieldName: 'create_by',
          dbLength: '64',
          dbPointLength: '0',
          dbDefaultVal: '',
          dbFieldTxt: '创建人',
          dbIsNull: true,
          dbType: 'string'
        },
        {
          dbFieldName: 'create_time',
          dbLength: '64',
          dbPointLength: '0',
          dbDefaultVal: '',
          dbFieldTxt: '创建时间',
          dbIsNull: true,
          dbType: 'string'
        },
        {
          dbFieldName: 'update_by',
          dbLength: '64',
          dbPointLength: '0',
          dbDefaultVal: '',
          dbFieldTxt: '修改人',
          dbIsNull: true,
          dbType: 'string'
        },
        {
          dbFieldName: 'update_time',
          dbLength: '64',
          dbPointLength: '0',
          dbDefaultVal: '',
          dbFieldTxt: '修改时间',
          dbIsNull: true,
          dbType: 'string'
        }
      ]
      state.dbPropertyList = arr
    }
    state.dbPropertyList.forEach(e => { e.index = state.i++ })
  },
  RESET_ARRAY(state, value) {
    console.log('当前数据', value)
  },
  RESET_INDEX(state, value) {
    state.indexMemory = value
    // const currRow = state.dbPropertyList.splice(value.oldIndex, 1)[0]
    // state.dbPropertyList.splice(value.newIndex, 0, currRow)
    state.i = 1
    state.dbPropertyList.forEach(e => { e.index = state.i++ })
  },
  SELECTED_CONTROLLER(state, value) { // 同步查询控件
    state.rowData = value
  },
  POSSESS_HEAD(state, value) {
    // 共享表头信息
    state.head = value
  },
  INIT_QUERY_ID(state, value) {
    // 共享表头信息
    state.tempId = value
  }
}

const actions = {
  addDbProperty(context, value) {
    context.commit('ADD_DB_PROPERTY', value)
  },
  delDbProperty(context, value) {
    context.commit('DEL_DB_PROPERTY', value)
  },
  initDbProperty(context, value) {
    context.commit('INIT_DB_PORPERTY', value)
  },
  resetArray(context, value) {
    context.commit('RESET_ARRAY', value)
  },
  resetIndex(context, value) {
    context.commit('RESET_INDEX', value)
  },
  selectedController(context, value) {
    context.commit('SELECTED_CONTROLLER', value)
  },
  possessHead(context, value) {
    context.commit('POSSESS_HEAD', value)
  },
  initQueryId(context, value) {
    context.commit('INIT_QUERY_ID', value)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
